/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Service;

import com.lcfgly.Dao.PriceImpl;
import com.lcfgly.Dao.Product;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Exception.ErrorException;
import com.lcfgly.Exception.SQLEmptyException;
import com.lcfgly.Plug.DataTableForShoprice;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author lcfgly
 */
@Service
public class ProductService implements Serializable{
    
    Logger log=Logger.getLogger("SqlError");
    
    @Autowired
    Product product;
    
    @Autowired
    PriceImpl price;
    
    @Autowired
    DataTableForShoprice dataTable;
    
    /**
     * 获取栏目列表（DataTable插件）数据
     * @param request
     * @param typeid
     * @param shopid
     * @return
     * @throws ActionException 
     */
    public Map GetProductListForDataTable(HttpServletRequest request , int typeid,int shopid) throws ActionException
    {
        try{
            HashMap out=new HashMap();
            out.put("name", "minpin_pinpai.name");
            return dataTable.GetDataForDataTable(request, "minpin_allshop", "minpin_allshop.id,name,title,shop_prices.price,minpin_allshop.litpic"," JOIN minpin_pinpai ON pinpai=minpin_pinpai.id LEFT JOIN shop_prices ON shop_prices.shopid="+shopid+" AND shop_prices.product=minpin_allshop.id", "minpin_allshop.typeid="+typeid, null,out);
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 获取栏目列表（DataTable插件）数据
     * @param request
     * @param shopid
     * @return
     * @throws ActionException 
     */
    public Map GetProductListForDataTable(HttpServletRequest request,int shopid) throws ActionException
    {
        try{
            HashMap out=new HashMap();
            out.put("name", "minpin_pinpai.name");
            return dataTable.GetDataForDataTable(request, "minpin_allshop", "minpin_allshop.id,minpin_allshop.typeid,name,title,shop_prices.price,minpin_allshop.litpic"," JOIN minpin_pinpai ON pinpai=minpin_pinpai.id LEFT JOIN shop_prices ON shop_prices.shopid="+shopid+" AND shop_prices.product=minpin_allshop.id","minpin_allshop.id>1", null,out);
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 获取栏目信息
     * @param typeid
     * @return 
     * @throws ActionException 
     */
    public Map GetTypeInfo(int typeid)throws ActionException{
        try{
            return product.GetTypeInfo(typeid);
        }catch(SQLEmptyException e){
            log.error(e);
            throw new ActionException("未查找到先关数据");
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 返回产品信息
     * @param typeid
     * @param id
     * @return
     * @throws ActionException 
     */
    public Map GetProductInfo(int typeid,int id) throws ActionException{
         try{
            return product.GetProductInfo(typeid, id);
        }catch(SQLEmptyException e){
            log.error(e);
            throw new ActionException("未查找到先关数据");
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 返回产品属性信息
     * @param addtable
     * @return
     * @throws ActionException 
     */
    public List GetProductProperty(String addtable)throws ActionException{
        try{
            return product.GetProductProperty(addtable);
        }catch(SQLEmptyException e){
            log.error(e);
            throw new ActionException("未查找到先关数据");
        }catch(SQLException e){
            log.error(e);
            throw new ErrorException();
        }
    }
    
    /**
     * 获取产品图片
     * @param id
     * @param addtable
     * @return 
     */
    public List GetProductImgs(int id,String addtable){
        try{
            return product.GetProductImgs(id,addtable);
        }catch(SQLException e){
            log.error(e);
            return null;
        }
    }
    
    /**
     * 获取shopid价格
     * @param shopid
     * @param idstring
     * @return 
     */
    public Map GetPriceForShopid(int shopid,String idstring){
        Map res=new HashMap();
        try{
            List sql=price.GetPriceForShopid(shopid,idstring);
            if(sql != null && sql.size()>0){
                for(Object re:sql){
                       Map r=(Map)re;
                       res.put("product"+r.get("product"), r.get("price"));
                }
            }
            return res;
        }catch(SQLException e){
            log.error(e);
            return res;
        }
    }
    
}
